package com.mango.iot.gateway.http.srv;

import com.alibaba.fastjson.JSONObject;
import com.mango.iot.gateway.http.consts.AppBizException;
import com.mango.iot.gateway.http.mq.KafkaProducer;
import com.mango.iot.gateway.http.mq.msg.HttpExchangeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 设备通信消息逻辑 Service
 *
 * @author liangfeihu
 * @since 2021/8/25 11:23
 */
@Slf4j
@Service
public class TopicService {

    @Autowired
    private KafkaProducer kafkaProducer;

    /**
     * <pre>
     * 处理业务消息
     * </pre>
     *
     * @param topic
     * @param jsonObject
     * @return void
     */
    public void handleUpMessage(String topic, JSONObject jsonObject) throws AppBizException {
        String payload = JSONObject.toJSONString(jsonObject);
        log.info("topic={}, payload={}", topic, payload);

        HttpExchangeMessage exchangeMessage = new HttpExchangeMessage();
        exchangeMessage.setTopic(topic);
        exchangeMessage.setPayload(payload);
        kafkaProducer.sendMessage("iot_http_event", exchangeMessage);
    }

}
